package com.lagou.controller;

import com.lagou.common.pojo.Result;
import com.lagou.service.ResumeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import static com.lagou.common.constant.Constant.*;

/**
 * @author tengfei wang
 * @version 1.0
 * @date Created in 2020/9/17
 */
@Controller
public class LoginController {


    @Resource
    private ResumeService resumeService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getAll(ModelMap modelMap) {
        Result resumeList = resumeService.getResumeList();
        modelMap.put("resumes", resumeList.getResult());
        return "resume";
    }

    @RequestMapping("/login")
    public String toLogin() {
        System.out.println("跳转登录页面");
        return "login";
    }

    @RequestMapping("/valid")
    public String valid(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession httpSession, ModelMap modelMap) {
        if (USERNAME.equals(username) && PASSWORD.equals(password)) {
            httpSession.setAttribute(TOKEN_NAME, TOKEN_VALUE);

            Result resumeList = resumeService.getResumeList();
            modelMap.put("resumes", resumeList.getResult());
            return "redirect:index";
        }
        System.out.println("用户名或密码错误!");
        return "redirect:login";
    }

    @RequestMapping("/index")
    public String index(ModelMap modelMap) {
        Result resumeList = resumeService.getResumeList();
        modelMap.put("resumes", resumeList.getResult());
        return "resume";
    }
}
